Move xcs to unix domain sockets.
signed-off-by: akw27@cl.cam.ac.uk
import socket
import time
-XCS_PORT = 1633
+XCS_PATH = "/var/xen/xcs_socket"
XCS_EXEC = "/usr/sbin/xcs"
XCS_LOGFILE = "/var/log/xcs.log"
""" See if the control switch is running.
"""
ret = 1
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
- s.connect( ("127.0.0.1", XCS_PORT) )
+ s.connect( (XCS_PATH) )
except:
ret = 0
s.close()
if (not xcs_running()):
if os.fork():
- time.sleep(1) # let xcs start
+ time.usleep(500) # let xcs start
else:
try:
logfile = os.open(XCS_LOGFILE,
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/socket.h>
+#include <sys/un.h>
#include <sys/mman.h>
#include <sys/poll.h>
#include <sys/sysmacros.h>
-#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
static int xcs_data_send(xcs_msg_t *msg);
static int xcs_data_read(xcs_msg_t *msg);
-static int xcs_connect(char *ip, short port)
+static int xcs_connect(char *path)
{
- struct sockaddr_in addr;
- int ret, flags;
+ struct sockaddr_un addr;
+ int ret, len, flags;
xcs_msg_t msg;
if (xcs_data_fd != -1) /* already connected */
return 0;
- xcs_ctrl_fd = socket(AF_INET, SOCK_STREAM, 0);
+ xcs_ctrl_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (xcs_ctrl_fd < 0)
{
printf("error creating xcs socket!\n");
goto fail;
}
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- addr.sin_addr.s_addr = inet_addr(ip);
- memset(&(addr.sin_zero), '\0', 8);
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, path);
+ len = sizeof(addr.sun_family) + strlen(addr.sun_path) + 1;
- ret = connect(xcs_ctrl_fd, (struct sockaddr *)&addr,
- sizeof(struct sockaddr));
+ ret = connect(xcs_ctrl_fd, (struct sockaddr *)&addr, len);
if (ret < 0)
{
printf("error connecting to xcs(ctrl)! (%d)\n", errno);
goto ctrl_fd_fail;
}
- //set_cloexec(xcs_ctrl_fd);
+ /*set_cloexec(xcs_ctrl_fd);*/
msg.type = XCS_CONNECT_CTRL;
msg.u.connect.session_id = xcs_session_id;
xcs_session_id = msg.u.connect.session_id;
/* now the data connection. */
- xcs_data_fd = socket(AF_INET, SOCK_STREAM, 0);
+ xcs_data_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (xcs_data_fd < 0)
{
printf("error creating xcs data socket!\n");
goto ctrl_fd_fail;
}
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- addr.sin_addr.s_addr = inet_addr(ip);
- memset(&(addr.sin_zero), '\0', 8);
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, path);
+ len = sizeof(addr.sun_family) + strlen(addr.sun_path) + 1;
- ret = connect(xcs_data_fd, (struct sockaddr *)&addr,
- sizeof(struct sockaddr));
+ ret = connect(xcs_data_fd, (struct sockaddr *)&addr, len);
if (ret < 0)
{
printf("error connecting to xcs(data)! (%d)\n", errno);
for (i = 0; i < XCS_RING_SIZE; i++)
REQ_RING_ENT(i) = RSP_RING_ENT(i) = NULL;
- (void)xcs_connect("127.0.0.1", XCS_TCP_PORT);
+ (void)xcs_connect(XCS_SUN_PATH);
return (PyObject *)xun;
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#include <sys/un.h>
#include <errno.h>
#include <malloc.h>
#include <fcntl.h>
static void map_dom_to_port(u32 dom, int port)
{
- if (dom >= dom_port_map_size) {
- dom_port_map = (int *)realloc(dom_port_map,
- (dom + 10) * sizeof(dom_port_map[0]));
-
- if (dom_port_map == NULL) {
- perror("realloc(dom_port_map)");
- exit(1);
- }
-
- for (; dom_port_map_size < dom + 10; dom_port_map_size++) {
- dom_port_map[dom_port_map_size] = -1;
- }
- }
+ if (dom >= dom_port_map_size) {
+ dom_port_map = (int *)realloc(dom_port_map,
+ (dom + 256) * sizeof(dom_port_map[0]));
+
+ if (dom_port_map == NULL) {
+ perror("realloc(dom_port_map)");
+ exit(1);
+ }
+
+ for (; dom_port_map_size < dom + 10; dom_port_map_size++) {
+ dom_port_map[dom_port_map_size] = -1;
+ }
+ }
- dom_port_map[dom] = port;
+ dom_port_map[dom] = port;
}
-static int dom_to_port(u32 dom) {
- if (dom >= dom_port_map_size) return -1;
+static int dom_to_port(u32 dom)
+{
+ if (dom >= dom_port_map_size) return -1;
- return dom_port_map[dom];
+ return dom_port_map[dom];
}
static void init_interfaces(void)
/* ------[ Simple helpers ]------------------------------------------------*/
/* listen_socket() is straight from paul sheer's useful select_tut manpage. */
-static int listen_socket (int listen_port)
+static int listen_socket (char *listen_path)
{
- struct sockaddr_in a;
+ struct sockaddr_un a;
int s;
int yes;
- if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0)
+ if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
{
perror ("socket");
return -1;
}
yes = 1;
- if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
- (char *) &yes, sizeof (yes)) < 0)
- {
- perror ("setsockopt");
- close (s);
- return -1;
- }
memset (&a, 0, sizeof (a));
- a.sin_port = htons (listen_port);
- a.sin_family = AF_INET;
+ a.sun_family = AF_UNIX;
+ strcpy(a.sun_path, listen_path);
+
+ /* remove an old socket if it exists. */
+ unlink(listen_path);
+
if (bind(s, (struct sockaddr *) &a, sizeof (a)) < 0)
{
perror ("bind");
close (s);
return -1;
}
- printf ("accepting connections on port %d\n", (int) listen_port);
+ printf ("accepting connections on path %s\n", listen_path);
listen (s, 10);
return s;
}
}
}
-int main (int argc, char*argv[])
+int main (int argc, char *argv[])
{
int listen_fd, evtchn_fd;
unbound_fd_t *unbound_fd_list = NULL, **ufd;
struct timeval timeout = { XCS_GC_INTERVAL, 0 };
connection_t **con;
-
+
/* Initialize xc and event connections. */
if (ctrl_chan_init() != 0)
{
init_interfaces();
init_bindings();
- listen_fd = listen_socket(XCS_TCP_PORT);
+ listen_fd = listen_socket(XCS_SUN_PATH);
/* detach from our controlling tty so that a shell does hang waiting for
stopped jobs. */
/* CASE 2: New connection on the listen port. */
if ( FD_ISSET ( listen_fd, &rd ))
{
- struct sockaddr_in remote_addr;
+ struct sockaddr_un remote_addr;
int size;
memset (&remote_addr, 0, sizeof (remote_addr));
size = sizeof remote_addr;
#ifndef __XCS_PROTO_H__
#define __XCS_PROTO_H__
-#define XCS_TCP_PORT 1633
+#define XCS_SUN_PATH "/var/xen/xcs_socket"
/* xcs message types: */
#define XCS_CONNECT_CTRL 0 /* This is a control connection. */
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#include <sys/un.h>
#include <ctype.h>
#include <xc.h>
#include <xen/xen.h>
static int xcs_ctrl_fd = -1; /* connection to the xcs server. */
static int xcs_data_fd = -1; /* connection to the xcs server. */
-int tcp_connect(char *ip, short port)
+int sock_connect(char *path)
{
- struct sockaddr_in addr;
- int ret, fd;
+ struct sockaddr_un addr;
+ int ret, len, fd;
- fd = socket(AF_INET, SOCK_STREAM, 0);
+ fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
{
printf("error creating xcs socket!\n");
return -1;
}
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- addr.sin_addr.s_addr = inet_addr(ip);
- memset(&(addr.sin_zero), '\0', 8);
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, path);
+ len = sizeof(addr.sun_family) + strlen(addr.sun_path) + 1;
- ret = connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr));
+ ret = connect(fd, (struct sockaddr *)&addr, len);
if (ret < 0)
{
printf("error connecting to xcs!\n");
return fd;
}
-void tcp_disconnect(int *fd)
+void sock_disconnect(int *fd)
{
close(*fd);
*fd = -1;
if ((strlen(argv[1]) >=2) && (strncmp(argv[1], "-v", 2) == 0))
verbose = 1;
- ret = tcp_connect("127.0.0.1", XCS_TCP_PORT);
+ ret = sock_connect(XCS_SUN_PATH);
if (ret < 0)
{
printf("connect failed!\n");
exit(-1);
}
- ret = tcp_connect("127.0.0.1", XCS_TCP_PORT);
+ ret = sock_connect(XCS_SUN_PATH);
if (ret < 0)
{
printf("connect failed!\n");